# -*- coding: utf-8 -*- 
# @Time : 2021/7/11 14:52 
# @Author : Dong 
# @File : demo02_test_api_flask.py

from flask import Flask, jsonify
from flask_restful import Resource,Api,reqparse


app = Flask(__name__)
api = Api(app,default_mediatype='application/json')

user = [
    {'name':'djy'}
]

class User(Resource):
    def get(self):
        print(user)
        return user

    def post(self):
        args = reqparse.RequestParser().add_argument("name",type=str,location='json',required=True,help="name is not None").parse_args()
        print(args)

        if args.values() not in [i.values() for i in user]:
            user.append({"name":args["name"]})
        return jsonify(user)

    def delete(self):
        global user
        user = []
        return jsonify(user)

class User_name(Resource):
    def get(self,user_id):
        try:
            user_name = {'name':user[int(user_id)-1].get("name")}
            print(user_name)
        except Exception as e:
            return jsonify({'ERROR':"None"})
        return jsonify(user_name)

api.add_resource(User,"/users")
api.add_resource(User_name,'/users/<user_id>')

if __name__ == '__main__':
    app.run(debug=True)